import { Callout } from "nextra/components"
import { TailoredExperience, LinkToCopilotCloud } from "@/components";
import SelfHostingCopilotRuntimeCreateEndpoint from "@/snippets/self-hosting-copilot-runtime-create-endpoint.mdx";
import SelfHostingCopilotRuntimeConfigureCopilotKitProvider from "@/snippets/self-hosting-copilot-runtime-configure-copilotkit-provider.mdx";

# Step 2: Setup CopilotKit

Now that we have our todo list app running, we're ready to integrate CopilotKit. For this tutorial, we will install the following dependencies:

- `@copilotkit/react-core`: The core library for CopilotKit, which contains the CopilotKit provider and useful hooks.
- `@copilotkit/react-textarea`: The textarea component for CopilotKit, which enables you to get instant context-aware autocompletions in your app.

## Install Dependencies

To install the CopilotKit dependencies, run the following:

```shell npm2yarn
npm install @copilotkit/react-core @copilotkit/react-textarea
```

## Setup CopilotKit
<TailoredExperience.Toggle />

<TailoredExperience.CloudContent>

  ### Configure the `<CopilotKit />` Provider

  The `<CopilotKit>` provider will wrap our app. Let's add it to the `page.tsx` file.

  <Callout type="info">
    <LinkToCopilotCloud>Click here to get your Copilot Cloud API key for free</LinkToCopilotCloud>. Then, replace `<your-public-api-key>` with your actual API key.
  </Callout>

  ```tsx filename="app/page.tsx" showLineNumbers {5-6,10,14}
  "use client";

  import { EmailThread } from "@/components/EmailThread";
  import { EmailsProvider } from "@/lib/hooks/use-email";
  import { CopilotKit } from "@copilotkit/react-core";
  import "@copilotkit/react-textarea/styles.css";

  export default function Home() {
    return (
      <CopilotKit publicApiKey={"<your-public-api-key>"}>
        <EmailsProvider>
          <EmailThread />
        </EmailsProvider>
      </CopilotKit>
    );
  }
  ```
</TailoredExperience.CloudContent>

<TailoredExperience.SelfHostContent>
  ### Step 1: Set up Copilot Runtime Endpoint

  <SelfHostingCopilotRuntimeCreateEndpoint />

  ### Step 2: Configure the CopilotKit Provider

  ```tsx filename="app/page.tsx" showLineNumbers {5,10,14}
  "use client";

  import { EmailThread } from "@/components/EmailThread";
  import { EmailsProvider } from "@/lib/hooks/use-email";
  import { CopilotKit } from "@copilotkit/react-core";
  import "@copilotkit/react-ui/styles.css";

  export default function Home() {
    return (
      <CopilotKit runtimeUrl={"<your-runtime-url>"}>
        <EmailsProvider>
          <EmailThread />
        </EmailsProvider>
      </CopilotKit>
    );
  }
  ```

</TailoredExperience.SelfHostContent>


Let's break this down:

- First, we imported the `CopilotKit` provider from `@copilotkit/react-core`.
- Then, we wrapped the page with the `<CopilotKit>` provider.
- We imported the built-in styles from `@copilotkit/react-textarea`. This is optional.

In the next step, we'll implement the AI-powered textarea as a replacement for our existing input component.